package com.bw.park.view

import android.graphics.Color
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_base.entity.ParkBeanItem
import com.bw.lib_base.mvvm.BaseActivity
import com.bw.park.ParkViewModel
import com.bw.park.R
import com.bw.park.databinding.ActivityParkAddBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/park/ParkAddActivity")
@AndroidEntryPoint
class ParkAddActivity : BaseActivity<ActivityParkAddBinding, ParkViewModel>() {
    var area = "A区停车场"
    var type = "标准车位"
    var areaId = 1
    var status = "空闲"
    override fun initView() {
        binding.parkaddRemark.filters = arrayOf(InputFilter.LengthFilter(10))
        binding.parkaddRemark.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val length = s?.length
                binding.parkaddNum.setText("${length}/10")
                if (length == 10){
                    binding.parkaddNum.setTextColor(Color.RED)
                }else{
                    binding.parkaddNum.setTextColor(Color.DKGRAY)
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }
        })

        val areas = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areas)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddArea.adapter = areaAdapter
        binding.parkaddArea.onItemSelectedListener = object : OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area = areas[position]
                areaId = position +1
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

        }

        val types = arrayOf("标准车位", "VIP车位")
        val typeAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, types)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddType.adapter = typeAdapter
        binding.parkaddType.onItemSelectedListener = object : OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type = types[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

        }

        binding.headOk.setOnClickListener {
            val carNo = binding.parkaddCarNo.text.toString()
            val remark = binding.parkaddRemark.text.toString()
            val carNum = binding.parkaddCarNum.text.toString()
            val name = binding.parkaddName.text.toString()
            val item = ParkBeanItem(areaId,area,null,carNum,remark,carNo,status,type,0,name)
            viewModel.addPark(item)
        }

        binding.parkaddRg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.parkadd_kx ->status = "空闲"
                R.id.parkadd_zy ->status = "已占用"
                R.id.parkadd_wh ->status = "维护中"
            }
        }
    }

    override fun initData() {
        viewModel.addParkSuc.observe(this){
            finish()
        }
    }

}